<?
session_start();
include 'php/connect.php';
include 'php/functions.php';
$amount = 3;

$output = " <script>				
				$(document).ready(function(){
					$('.show-spec').click(function(e) {	
						var clicked = jQuery(e.target);
						var id = clicked.attr('id');
						editSpec(id);
					});
					$('.show-user').click(function(e) {	
						var clicked = jQuery(e.target);
						var id = clicked.attr('id');
						getUser(id);
					});
				});
				$(function(){
					$('.ratings').children().not(':radio').hide();
					$('.ratings').stars({
						disabled: true
					});
				});
			</script>
				<table id='hot_specs' class='content-subtitle-table'>";
	
				$abfrage = "SELECT spec.specId
					   ,spec.userId
					   ,spec.layout
					   ,spec.jobId
					   ,spec.level
					   ,job.tri as jobTri
					   ,job.name as jobName
					   ,spec.description
					   ,spec.public
					   ,spec.cdate
					   ,spec.udate
					   ,rate.value
					   ,rate.count
					   ,user.userName
				FROM (((
				ffxivc_specs as spec 
				LEFT JOIN 
				ffxivc_jobs as job
				ON job.jobId = spec.jobId)
				LEFT JOIN ffxivc_users as user
				ON user.userId = spec.userId) 
				LEFT JOIN (SELECT specId, sum(rat.value) as value, count(rat.rateId) as count FROM ffxivc_ratings as rat group by rat.specId) as rate
				ON spec.specId = rate.specId)
				WHERE spec.public = 'Y'
				ORDER BY rate.count DESC
				LIMIT 0,".$amount."
				";
				
$res = mysql_query($abfrage);
$num = mysql_num_rows($res);	
$i = 1;
$output .= "	
		<tr id='tr_".$row['specId']."' title='".$row['description']."' class='spec-row tr-table'>
			<td class='td-table vertical-middle pad-5'>
				<div style='height:15px;' colspan='5'>
					<div class=' normal-text blue-text' style='height: 15px;' >Hottest specs</div>
				</div>
			</td>
		</tr>";
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
	$count = $row['count'];
	$value = $row['value'];
	if($count != 0){
		$average = floor($value / $count);
	}else{
		$average = 0;
	}
	$output .= "
		<tr id='tr_".$row['specId']."' title='".$row['description']."' class='spec-row tr-table ";
		if($i!=$amount){
			$output .= "border-bottom";
		}
		$output .= "' name='".$row['specId']."'>
				<td class='td-table vertical-middle pad-5'>
				<div style='height:15px;'>
					<div id='".$row['userName']."' class='show-user cur-point normal-text blue-text' style='width: 90px;overflow: hidden;height: 15px;' >".$row['userName']."</div>
				</div>
			</td>
			<td class='td-table vertical-middle pad-5' style='padding-left: 20px'><div class='normal-text' style='width: 25px;'>".$row['level']."</div></td>
			<td class='td-table vertical-middle pad-5'><img style='margin-right: 12px;' src='media/jobIcons/B_".$row['jobTri'].".png' /></td>
			<td class='td-table vertical-middle pad-5'>
					<form class='ratings' action='' method='post' style='width: 80px;' title='Rating: ".$average.", Votes: ".$row['count']."'>";

					foreach (get_options() as $id => $title){
						$output .= 	"<input type='radio' name='rate'  value='".$id."' id='rate".$id."'";
						if($id==$average){
							$output .= 	" checked";
						}
						$output .= 	" />";
					}
			
					$output .= 	"</form>
			</td>
			<td id='publish-spec-toggle' class='td-table vertical-middle pad-5' name='no'>";
			
	
	$output .= "<a id='".$row['specId']."' class='show-spec fl-right horizontal-right pad-5 font-10 cur-point' style='width: 45px;' name='no' >Show</a>";
	$output .=" </td>
		</tr>";
	
	$i++;
	
}


mysql_free_result($res);

echo $output;

?>